package com.dmgis.hndz.common;


import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.client.*;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;


public class MongoTest {

    @Test
    public void upload() {
        upload("C:/Users/PC/Desktop/uc/grid.grid");
    }

    @Test
    public void download() {
        download("ceshi", "1.png", "C:/Users/PC/Desktop/cu/grid.grid");
//        download("5e4be8c63930f446e42b8c65","C:/Users/zhangli/Desktop/1.png");
    }

    public static void upload(String filepath) {
        final String MONGODB_URI = "mongodb://192.168.0.143:27017";
        final String DATABASE_NAME = "tf";
        final String COLLECTION_NAME = "ceshi";

        MongoClient mongoClient = MongoClients.create(MONGODB_URI);
        try {
            MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
            MongoCollection<Document> collection = db.getCollection(COLLECTION_NAME);

            File file = new File(filepath);
            if (!file.exists()) {
                System.err.println("File does not exist: " + filepath);
                return;
            }

            Document doc = new Document();
            doc.append("filename", file.getName())
                    .append("content", Files.readAllBytes(file.toPath()));

            collection.insertOne(doc);

        } catch (IOException e) {
            System.err.println("Error reading file: " + filepath);
            e.printStackTrace();
        } finally {
            mongoClient.close();
        }
    }

    public static void download(String collectionName, String fileName, String outputPath) {
        final String MONGODB_URI = "mongodb://192.168.0.143:27017";
        final String DATABASE_NAME = "tf";

        MongoClient mongoClient = MongoClients.create(MONGODB_URI);
        try {
            MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
            MongoCollection<Document> collection = db.getCollection(collectionName);

            Document query = new Document("filename", fileName);
            FindIterable<Document> findResult = collection.find(query);

            for (Document doc : findResult) {
                org.bson.types.Binary binaryContent = doc.get("content", org.bson.types.Binary.class);
                byte[] content = binaryContent.getData();

                File outputFile = new File(outputPath);
                Files.write(outputFile.toPath(), content);

                System.out.println("File downloaded successfully to: " + outputPath);
            }

        } catch (IOException e) {
            System.err.println("Error writing file to: " + outputPath);
            e.printStackTrace();
        } finally {
            mongoClient.close();
        }
    }


    @Test
    public void uploadT() {
        uploadT("C:/Users/PC/Desktop/uc/1.png");
    }

    @Test
    public void downloadT() {
        downloadT("5e4be8c63930f446e42b8c65","C:/Users/PC/Desktop/cu/1.png");
    }

    public static void downloadT(String id,String filepath) {
        try {
            Mongo db = new Mongo("192.168.0.143", 27017);
            DB mydb = db.getDB("tf");
            GridFS myFS = new GridFS(mydb);
            GridFSDBFile gridFSDBFile = myFS.findOne(new ObjectId(id));
            gridFSDBFile.writeTo(filepath);
            db.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void uploadT(String filepath) {
        try (MongoClient mongoClient = MongoClients.create("mongodb://192.168.0.143:27017")) {
            MongoDatabase db = mongoClient.getDatabase("tf");
            GridFSBucket gridFSBucket = GridFSBuckets.create(db, "ceshi");

            File file = new File(filepath);
            ObjectId fileId = gridFSBucket.uploadFromStream(file.getName(), new FileInputStream(file));

            System.out.println(fileId.toHexString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

